import {
  Model
} from 'cesium'
import DrawCommand from 'cesium/Source/Renderer/DrawCommand.js'

/**
 * Primitive Postprocess
 *
 * @param owner Cammand
 * @returns {*} postProcess object
 */
const primitivePostprocess = (owner) => {
  return owner && owner.postProcess ? owner.postProcess : null;
}

const modelPostprocess = (owner) => {
  return owner &&
    owner.primitive &&
    owner.primitive instanceof Model &&
    owner.primitive.postProcess ?
    owner.primitive.postProcess :
    null;
}

const rewrite = function () {
  /**
   * DrawCommand Extend
   * For Primitive Postgress
   *
   * @param context context
   * @param passState state
   */
  DrawCommand.prototype.execute = function (context, passState) {
    let cammand = this;
    let postprocess;

    if ((postprocess = primitivePostprocess(cammand.owner))) {
      postprocess(cammand, context, passState);
    } else if ((postprocess = modelPostprocess(cammand.owner))) {
      postprocess(cammand, context, passState);
    } else {
      context.draw(cammand, passState);
    }
  };
}

export default rewrite